<template>
    <div class="background">
        <div style="width: 400px; margin: 25vh auto">
            <div style="color: rgb(255,255,255); font-size: 30px; text-align: center; font-weight: bold; padding: 30px 0">欢迎注册</div>
            <el-form :model="form" ref="form" size="normal" :rules="rules">
                <el-form-item prop="username">
                    <el-input v-model="form.username" >
                        <template #prefix>
                            <el-icon class="el-input__icon"><UserFilled /></el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="form.password" type="password" show-password>
                        <template #prefix>
                            <el-icon class="el-input__icon"><Lock /></el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="confirm">
                    <el-input v-model="form.confirm" type="password" show-password>
                        <template #prefix>
                            <el-icon class="el-input__icon"><Lock /></el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item style="padding-left: 120px">
                    <el-row>
                        <el-button type="primary" @click="register">注&nbsp;&nbsp;&nbsp;册</el-button>
                        <el-button type="primary" @click="login">登&nbsp;&nbsp;&nbsp;录</el-button>
                    </el-row>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    import request from "../utils/request";

    export default {
        name: "Register",
        data () {
            return {
                form: {},
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                    ],
                    confirm: [
                        { required: true, message: '请确认密码', trigger: 'blur' },
                    ],
                }
            }
        },
        methods: {
            register() {
                if (this.form.password !== this.form.confirm){
                    this.$message({
                        type: "error",
                        message: '两次密码输入不一致！'
                    })
                    // alert("两次密码输入不一致！")
                    return
                }

                this.$refs['form'].validate((valid) => {
                    if (valid){
                        request.post("/user/register", this.form).then(res => {
                            if (res.code === '0'){
                                this.$message({
                                    type: "success",
                                    message: "注册成功"
                                })
                                this.$router.push("/login")   //注册成功之后进行页面跳转，跳转到登录界面
                            } else {
                                this.$message({
                                    type: "error",
                                    message: res.msg
                                })
                            }
                        })
                    }
                })
            },
            login() {
                this.$router.push("/login")
            }
        }
    }
</script>

<style scoped>
    .background{
        width:100%;
        height:100%;
        background: url("../assets/login/bgimg.jpg") center;
        /*-webkit-filter: blur(5px);*/
        /*filter: blur(15px);*/
        position: fixed;
        z-index: -1;
        /*background-size:100% 100%;*/
    }
    ::v-deep(.el-input__inner) {
    }
</style>